package com.comum.javahomework420242025.repository;

import com.comum.javahomework420242025.pojo.Approval;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface ApprovalRepository extends JpaRepository<Approval, Long> {


    // 模糊匹配id、name和score字段
    @Query("select c from Approval c where " +
            "c.userid=:userid and (" +
            "cast(c.contract.id as string) like %:str% or c.contract.name like %:str% or " +
            "c.sjjd like %:str% or " +
            "c.scjd like %:str% or c.zpjd like %:str% or " +
            "c.zzjd like %:str% )")
    Page<Approval> list(String str, String userid, Pageable pageable);

    boolean existsByContractIdAndUserId(Long contractid, Long userid);

    @Query("select a from Approval a where a.contractid=:str")
    List<Approval> getApprovalByContractId(@Param("str") String str);


    @Modifying
    @Transactional
    @Query("DELETE FROM Approval t WHERE t.contractid = :contractid AND t.userid = :userid")
    void deleteByContractidAndUserid(Long contractid, Long userid);
}